package pw.yumc.Residence.runnable;

import org.bukkit.entity.Damageable;
import org.bukkit.entity.Player;

import com.bekvon.bukkit.residence.protection.ClaimedResidence;

import cn.citycraft.PluginHelper.utils.CompatibleUtil;
import pw.yumc.Residence.ResidenceMain;

public class HealTask implements Runnable {
    ResidenceMain plugin;

    public HealTask(final ResidenceMain plugin) {
        this.plugin = plugin;
    }

    @Override
    public void run() {
        try {
            for (final Player player : CompatibleUtil.getOnlinePlayers()) {
                final String resname = plugin.getPlayerListener().getCurrentResidenceName(player.getName());
                ClaimedResidence res = null;
                if (resname != null) {
                    res = plugin.getResidenceManager().getByName(resname);
                }
                if (res != null && res.getPermissions().has("healing", false)) {
                    final Damageable damage = player;
                    final double health = damage.getHealth();
                    if (health < 20 && !player.isDead()) {
                        player.setHealth(health + 1);
                    }
                }
            }
        } catch (final Exception ex) {
        }
    }

}
